<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分账订单" prop="altOrderNo">
        <el-input
          v-model="queryParams.altOrderNo"
          placeholder="请输入分账订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分账状态" prop="altOrderStatus">
        <el-select v-model="queryParams.altOrderStatus" placeholder="请选择分账状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.alt_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发起时间" prop="requestTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.requestTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发起时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
        <!--          v-hasPermi="['deduction:order:add']"-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
        <!--          v-hasPermi="['deduction:order:edit']"-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
        <!--          v-hasPermi="['deduction:order:remove']"-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
        <!--          v-hasPermi="['deduction:order:export']"-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分账订单号" align="center" prop="altOrderNo" />
      <el-table-column label="商户名" align="center" prop="tenantName" />
      <el-table-column label="订单分账剩余金额" align="center" prop="totalOrderAmount" />
      <el-table-column label="分账总金额" align="center" prop="altOrderAmount" />
      <el-table-column label="订单手续费" align="center" prop="altOrderAmountFee" />
      <el-table-column label="分账状态" align="center" prop="altOrderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alt_order_status" :value="scope.row.altOrderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="分账失败说明" align="center" prop="failDesc" />
      <el-table-column label="发起时间" align="center" prop="requestTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.requestTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <!--            v-hasPermi="['deduction:order:edit']"-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--          >删除</el-button>-->
          <!--            v-hasPermi="['deduction:order:remove']"-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改扣款分账订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="扣款分账订单code" prop="deductionAltOrderCode">
          <el-input v-model="form.deductionAltOrderCode" placeholder="请输入扣款分账订单code" />
        </el-form-item>
        <el-form-item label="扣款分账id" prop="deductionAltId">
          <el-input v-model="form.deductionAltId" placeholder="请输入扣款分账id" />
        </el-form-item>
        <el-form-item label="分账批次" prop="deductionAltOrderBatch">
          <el-input v-model="form.deductionAltOrderBatch" placeholder="请输入分账批次" />
        </el-form-item>
        <el-form-item label="分账订单号" prop="altOrderNo">
          <el-input v-model="form.altOrderNo" placeholder="请输入分账订单号" />
        </el-form-item>
        <el-form-item label="订单分账剩余金额" prop="totalOrderAmount">
          <el-input v-model="form.totalOrderAmount" placeholder="请输入订单分账剩余金额" />
        </el-form-item>
        <el-form-item label="分账总金额" prop="altOrderAmount">
          <el-input v-model="form.altOrderAmount" placeholder="请输入分账总金额" />
        </el-form-item>
        <el-form-item label="订单手续费" prop="altOrderAmountFee">
          <el-input v-model="form.altOrderAmountFee" placeholder="请输入订单手续费" />
        </el-form-item>
        <el-form-item label="分账状态">
          <el-radio-group v-model="form.altOrderStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分账失败说明" prop="failDesc">
          <el-input v-model="form.failDesc" placeholder="请输入分账失败说明" />
        </el-form-item>
        <el-form-item label="业务通知地址" prop="businessCallbackUrlAlt">
          <el-input v-model="form.businessCallbackUrlAlt" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="平台回调地址" prop="callbackUrlAlt">
          <el-input v-model="form.callbackUrlAlt" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="发起时间" prop="requestTime">
          <el-date-picker clearable size="small"
            v-model="form.requestTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发起时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="请求数据" prop="requestData">
          <el-input v-model="form.requestData" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="回调处理时间" prop="callbackTime">
          <el-date-picker clearable size="small"
            v-model="form.callbackTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择回调处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="返回记录" prop="callbackData">
          <el-input v-model="form.callbackData" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商户ID" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入商户ID" />
        </el-form-item>
        <el-form-item label="商户code" prop="tenantCode">
          <el-input v-model="form.tenantCode" placeholder="请输入商户code" />
        </el-form-item>
        <el-form-item label="商户名" prop="tenantName">
          <el-input v-model="form.tenantName" placeholder="请输入商户名" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/deduction/altorder";

export default {
  name: "Order",
  dicts: ['alt_order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 扣款分账订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deductionAltOrderCode: null,
        deductionAltId: null,
        deductionAltOrderBatch: null,
        altOrderNo: null,
        totalOrderAmount: null,
        altOrderAmount: null,
        altOrderAmountFee: null,
        altOrderStatus: null,
        failDesc: null,
        businessCallbackUrlAlt: null,
        callbackUrlAlt: null,
        requestTime: null,
        requestData: null,
        callbackTime: null,
        callbackData: null,
        tenantId: null,
        tenantCode: null,
        tenantName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deductionAltOrderCode: [
          { required: true, message: "扣款分账订单code不能为空", trigger: "blur" }
        ],
        deductionAltId: [
          { required: true, message: "扣款分账id不能为空", trigger: "blur" }
        ],
        deductionAltOrderBatch: [
          { required: true, message: "分账批次不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询扣款分账订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deductionAltOrderId: null,
        deductionAltOrderCode: null,
        deductionAltId: null,
        deductionAltOrderBatch: null,
        altOrderNo: null,
        totalOrderAmount: null,
        altOrderAmount: null,
        altOrderAmountFee: null,
        altOrderStatus: "0",
        failDesc: null,
        businessCallbackUrlAlt: null,
        callbackUrlAlt: null,
        requestTime: null,
        requestData: null,
        callbackTime: null,
        callbackData: null,
        tenantId: null,
        tenantCode: null,
        tenantName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deductionAltOrderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加扣款分账订单";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      const deductionAltOrderId = row.deductionAltOrderId;
      this.$router.push("/deduction/altSecondDetail/altOrderDetail/" + deductionAltOrderId);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deductionAltOrderId = row.deductionAltOrderId || this.ids
      getOrder(deductionAltOrderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改扣款分账订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deductionAltOrderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deductionAltOrderIds = row.deductionAltOrderId || this.ids;
      this.$modal.confirm('是否确认删除扣款分账订单编号为"' + deductionAltOrderIds + '"的数据项？').then(function() {
        return delOrder(deductionAltOrderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有扣款分账订单数据项？').then(() => {
        this.exportLoading = true;
        return exportOrder(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
